# -*- coding:utf8 -*-

__author__ = 'Forcs'

from django.db import models

import datetime

import wanplus.config as wp_config

class BaseModel(models.Model):

    def to_json(self):
        fields = self._meta.get_fields()
        json = dict()
        for field in fields:
            val = getattr(self, field.name)
            if isinstance(val, datetime.datetime):
                try:
                    json[field.name] = val.strftime('%Y-%m-%d %H:%M:%S')
                except Exception, e:
                    json[field.name] = val.strftime('%Y-%m-%dT%H:%M:%S.%fZ')

            elif isinstance(val, datetime.date):
                json[field.name] = val.strftime('%Y-%m-%d')

            else:
                json[field.name] = val

        return json

    class Meta:
        abstract = True
        app_label = wp_config.DATABASE
